﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Data_Access_Layer;
using OnlineStore.Business_Logic_Layer;
namespace OnlineStore
{
    public partial class frmPurchase : Form
    {
        public frmPurchase()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            BizPurchase eBizPruchase  = new BizPurchase();
            int nCheckValidate = 0;
            nCheckValidate = eBizPruchase.ValidateData(txtPurchaseID.Text, dgPurchaseDetail.Rows);
            if (nCheckValidate == -1)
            {
                MessageBox.Show("Purchase ID must be number...");
                return;
            }
            if (nCheckValidate == -2)
            {
                MessageBox.Show("Purchase ID is existed...");
                return;
            }

            if (nCheckValidate == 0)
                eBizPruchase.SavePurchase(txtPurchaseID.Text, txtSuppName.Text, txtSuppAddress.Text, txtShipTo.Text, dCreatedDate.Text, dgPurchaseDetail.Rows, txtStatus.Text);
        }

        private void dgPurchaseDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {            
            try{
                if(e.ColumnIndex == 0)
                    if (dgPurchaseDetail.Rows[e.RowIndex].Cells[0].Value.ToString() != "") // Enter product ID
                    {                        
                        BizProduct eBizProduct = new BizProduct();
                        tbProduct eProduct = new tbProduct();
                        List<tbProduct> eListProduct = new List<tbProduct>();
                        eListProduct = eBizProduct.GetProductbyKeyWord("ID", dgPurchaseDetail.Rows[e.RowIndex].Cells[0].Value.ToString());
                        if (eListProduct.Count == 0)
                        {
                            MessageBox.Show("This product item is not existed...");
                            return;
                        }
                        eProduct = eListProduct.First();
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[1].Value = eProduct.Product_Name;
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[2].Value = eProduct.Color;
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[3].Value = eProduct.Size;
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[4].Value = "1";
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[5].Value = eProduct.Price;
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[6].Value = eProduct.Price;
                        txtTotal.Text = CalTotal();
                    }
                }
            catch(Exception ex)
            {                
                ex.ToString();
            }

            try{
                if(e.ColumnIndex == 4) // Enter purchase qty
                    if (dgPurchaseDetail.Rows[e.RowIndex].Cells[0].Value.ToString() != "")
                    {
                        int nQty = int.Parse(dgPurchaseDetail.Rows[e.RowIndex].Cells[4].Value.ToString());
                        int nPrice = int.Parse(dgPurchaseDetail.Rows[e.RowIndex].Cells[5].Value.ToString());
                        int nTotal = nQty * nPrice;
                        dgPurchaseDetail.Rows[e.RowIndex].Cells[6].Value = nTotal.ToString();
                        txtTotal.Text = CalTotal();
                    }               
            }
            catch(Exception ex)
            {
                if (ex.Message == "Input string was not in a correct format.")
                    MessageBox.Show("Qty must be number...");

                ex.ToString();
            }
        }

        private string CalTotal()
        {
            double dTotal = 0;
            try
            {                
                for (int i = 0; i < dgPurchaseDetail.Rows.Count; i++)
                {
                    dTotal += double.Parse(dgPurchaseDetail.Rows[i].Cells[6].Value.ToString());
                }
             
            }
            catch (Exception ex)
            {
                ex.ToString();                
            }
            return dTotal.ToString();
        }
        private void frmPurchase_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
     
    }
}
